<template>
  <div class="shopcar">
    <div class="type">
      <span class="iconfont" @click="goto()">&#xe779;</span>
      购物车
      <router-link to="/home/search"
        ><span class="iconfont">&#xe86e;</span></router-link
      >
    </div>
    <div class="bot">
      <div class="xz">
        <span class="iconfont" :class="qx ? 'active' : ''" @click="isqx()"
          >&#xe615;</span
        >
        <p>全选</p>
      </div>
      <p>总计:￥{{ zongjia }}</p>
      <div class="bj">
        <p @click="bj()">{{ wc }}</p>
        <p v-if="bianji" @click="remove()">删除</p>
        <p v-if="!bianji" @click="jiesuan(), tijiao()">结算</p>
      </div>
    </div>
    <div class="if" v-if="myif">购物车还没有东西，快去逛逛吧~</div>
    <div class="mycar">
      <div class="xz">
        <span class="iconfont" :class="qx ? 'active' : ''" @click="isqx()"
          >&#xe615;</span
        >
        <p>全选</p>
      </div>
      <div class="car" v-for="(item, index) in gouwuche">
        <div class="left">
          <span
            class="iconfont"
            :class="item.xz ? 'active' : ''"
            @click="xd(index)"
            >&#xe615;</span
          >
        </div>
        <img :src="item.img" alt="" />
        <div class="right">
          <router-link
            :to="{ path: '/home/commodity', query: { id: item.id } }"
          >
            <p>{{ item.name }}</p>
          </router-link>
          <div class="gg">
            <span v-if="item.gg">规格:{{ item.gg }}</span>
            <span v-if="item.ys">颜色:{{ item.ys }}</span>
          </div>
          <div class="jg">
            <span>￥{{ item.jg * item.sl }}</span>
            <div class="sl">
              <span @click="res(index)">-</span>
              <span>{{ item.sl }}</span>
              <span @click="add(index)">+</span>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<style lang="scss" scoped>
.shopcar {
  .bot {
    position: fixed;
    display: flex;
    bottom: 60px;
    height: 50px;
    line-height: 50px;
    width: 100%;
    z-index: 99999;
    border-top: 1px solid #ccc;
    border-bottom: 1px solid rgb(228, 228, 228);
    background-color: white;
    .xz {
      display: flex;
      padding: 10px;
      > span {
        display: block;
        width: 25px;
        height: 25px;
        border-radius: 50%;
        text-align: center;
        font-size: 18px;
        font-weight: bolder;
        color: white;
        line-height: 25px;
        border: 1px solid #ccc;
        background-color: white;
      }
      p {
        margin-left: 15px;
        color: rgb(68, 68, 68);
        line-height: 30px;
        font-size: 15px;
      }
      .active {
        background-color: rgb(255, 94, 0);
        border: 1px solid white;
      }
    }
    > p {
      font-size: 15px;
      color: red;
      margin-left: 10px;
    }
    .bj {
      position: absolute;
      display: flex;
      right: 0px;
      p {
        font-size: 15px;
        padding: 0 20px;
        color: white;
        background-color: rgb(36, 87, 255);
      }
      p:nth-child(2) {
        background-color: red;
      }
    }
  }
  .type {
    text-align: center;
    line-height: 50px;
    font-size: 16px;
    position: fixed;
    width: 100%;
    top: 0;
    z-index: 9999;
    border-bottom: 1px solid #ccc;
    font-weight: bold;
    color: rgb(116, 116, 116);
    text-align: center;
    background-color: white;
    span {
      position: absolute;
      display: block;
      height: 50px;
      width: 40px;
    }
    a {
      display: block;
      span {
        position: absolute;
        display: block;
        height: 50px;
        color: rgb(116, 116, 116);
        font-size: 25px;
        width: 40px;
        right: 0;
        top: 0;
      }
    }
  }
  .if {
    position: absolute;
    color: red;
    font-size: 16px;
    left: 60px;
    margin-top: 90px;
  }
  .mycar {
    padding-bottom: 100px;
    .xz {
      margin-top: 50px;
      display: flex;
      padding: 10px;
      border-bottom: 1px solid #ccc;
      > span {
        display: block;
        width: 25px;
        height: 25px;
        border-radius: 50%;
        text-align: center;
        font-size: 18px;
        font-weight: bolder;
        color: white;
        line-height: 25px;
        border: 1px solid #ccc;
        background-color: white;
      }
      p {
        margin-left: 15px;
        color: rgb(68, 68, 68);
        line-height: 30px;
        font-size: 16px;
      }
      .active {
        background-color: rgb(255, 94, 0);
        border: 1px solid white;
      }
    }
    .car {
      display: flex;
      margin: 10px;
      height: 100px;
      overflow: hidden;
      border-bottom: 1px solid #ccc;
      img {
        width: 80px !important;
        max-height: 90px;
        margin: 0 10px;
      }
      .right {
        p {
          font-size: 15px;
          color: rgb(70, 70, 70);
          white-space: nowrap;
          overflow: hidden;
          text-overflow: ellipsis;
        }
        .gg {
          display: flex;
          margin: 15px 0;
          span {
            color: rgb(138, 138, 138);
            font-size: 12px;
            margin-right: 10px;
          }
        }
        .jg {
          display: flex;
          margin-top: 15px;
          position: relative;
          > span {
            font-size: 14px;
            color: red;
            margin-top: 5px;
          }
          .sl {
            position: absolute;
            display: flex;
            right: 10px;
            span {
              margin: 0 2px;
              text-align: center;
              line-height: 30px;
              width: 30px;
              height: 30px;
              background-color: rgb(219, 219, 219);
              color: rgb(138, 138, 138);
            }
            span:nth-child(2) {
              font-size: 16px;
              width: 45px;
              color: black;
            }
          }
        }
      }
      .left {
        span {
          margin-top: 35px;
          display: block;
          width: 25px;
          height: 25px;
          border-radius: 50%;
          text-align: center;
          font-size: 18px;
          font-weight: bolder;
          color: white;
          line-height: 25px;
          border: 1px solid #ccc;
          background-color: white;
        }
        .active {
          background-color: rgb(255, 94, 0);
          border: 1px solid white;
        }
      }
    }
  }
}
</style>
<script>
import { mapState, mapMutations } from "vuex";
export default {
  name: "shopcar",
  data() {
    return {
      qx: false,
      zongjia: 0,
      // 是否进入编辑状态
      bianji: false,
      wc: "编辑",
      // 允许跳转
      tz: false,
      myif: true,
    };
  },
  methods: {
    // 返回上一级
    // 全选
    goto(){
      this.$router.go(-1);
    },
    isqx() {
      if (this.qx) {
        this.qx = false;
        for (let i = 0; i < this.gouwuche.length; i++) {
          this.gouwuche[i].xz = false;
          this.zongjia = 0;
        }
      } else {
        this.qx = true;
        for (let i = 0; i < this.gouwuche.length; i++) {
          if (!this.gouwuche[i].xz) {
            this.gouwuche[i].xz = true;
            this.zongjia += this.gouwuche[i].jg * this.gouwuche[i].sl;
          }
        }
      }
    },
    // 编辑
    bj() {
      if (this.bianji) {
        this.bianji = false;
        this.wc = "编辑";
      } else {
        this.bianji = true;
        this.wc = "完成";
      }
    },
    // 结算
    jiesuan() {
      this.tz = false;
      for (let i = 0; i < this.gouwuche.length; i++) {
        if (this.gouwuche[i].xz) {
          this.tz = true;
          this.tijiaodd.push(this.gouwuche[i]);
          this.gouwuche.splice(i, 1);
          i--;
        }
      }
      if (this.tz) {
        for (let i = 0; i < this.gouwuche.length; i++) {
          this.gouwuche[i].xz = false;
          this.zongjia = 0;
        }
        if (this.gouwuche.length == 0) {
          this.myif = true;
        } else {
          this.myif = false;
        }
        this.$router.push({ path: "/settlement" });
      }
    },
    // 删除
    remove() {
      for (let i = 0; i < this.gouwuche.length; i++) {
        if (this.gouwuche[i].xz) {
          this.gouwuche.splice(i, 1);
          this.zongjia = 0;
        }
      }
    },
    // 数量+
    add(i) {
      this.gouwuche[i].sl++;
      if(this.gouwuche[i].xz){
         this.zongjia+=this.gouwuche[i].jg
      }
    },
    // 数量-
    res(i) {
      if (this.gouwuche[i].sl >= 2) {
        this.gouwuche[i].sl--;
         if(this.gouwuche[i].xz){
        this.zongjia-=this.gouwuche[i].jg
         }
      }
    },
    // 勾选下单
    xd(i) {
      if (this.gouwuche[i].xz) {
        this.qx = false;
        this.gouwuche[i].xz = false;
        this.zongjia -= this.gouwuche[i].jg * this.gouwuche[i].sl;
      } else {
        this.gouwuche[i].xz = true;
        this.zongjia += this.gouwuche[i].jg * this.gouwuche[i].sl;
      }
    },
    ...mapMutations(["tijiao"]),
  },
  computed: {
    ...mapState(["gouwuche", "tijiaodd"]),
  },
  mounted() {
    if (this.gouwuche.length == 0) {
      this.myif = true;
    } else {
      this.myif = false;
    }
  },
  beforeUpdate() {
    if (this.gouwuche.length == 0) {
      this.myif = true;
    } else {
      this.myif = false;
    }
  },
};
</script>